package com.ifast.asset.quartz;

import com.alibaba.fastjson.JSONObject;
import com.ifast.asset.dao.AssetsDao;
import com.ifast.asset.domain.Assets;
import com.ifast.asset.domain.TaskrecordDO;
import com.ifast.asset.util.NmapExecuteUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Component
public class StartOfDayJob implements Job {

    @Resource
    AssetsDao assetsDao;
    @Resource
    private NmapExecuteUtil nmapExecuteUtil;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        System.out.println("执行定时函数了。。。。");
        String code = jobExecutionContext.getJobDetail().getJobDataMap().getString("code");
        JSONObject object = JSONObject.parseObject(code);
        TaskrecordDO taskrecord = JSONObject.toJavaObject(object, TaskrecordDO.class);
        if (taskrecord.getGroupId() != null && !"".equals(taskrecord.getGroupId())) {
            List<Assets> assets = assetsDao.saveByGroup(taskrecord.getGroupId());
            for (int i = 0; i < assets.size(); i++) {

                taskrecord.setIp(assets.get(i).getIp());
                nmapExecuteUtil.execute(taskrecord);
            }
        } else {

            //单次执行
            nmapExecuteUtil.execute(taskrecord);
        }
    }
}
